import {baseURL} from '../service/config'
//同时加载ajax次数
let ajaxTime=0;
export default function request (options) {

  //判断 请求路径中是否带/my/，请求私有路径 带上header：token
  let header={...options.header}
  if(options.url.includes("/my/")){
    //拼接header 带上token
    header["Authorization"]=wx.getStorageSync('token')
  }



  ajaxTime++
  //显示加载中
  wx.showLoading({
    title: '加载中',
  })

  return new Promise((resolve,reject)=>{
    wx.request({
      url: baseURL+options.url,
      header:header,
      method:options.method||'get',
      data:options.data||{},
      success:resolve,
      fail:reject,
      complete:(err)=> {
        ajaxTime--
        if(ajaxTime===0){

          //关闭正在加载的效果
          wx.hideLoading()
        }
       
      }
    })
  })
}